/*!
 * liScroll 1.0
 * Examples and documentation at: 
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
 * 2007-2010 Gian Carlo Mingati
 * Version: 1.0.2.1 (22-APRIL-2011)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.2.x or later
 * 
 */
// dropdown to select box plugin

// -----------------------------

!function() {

 

  var $dropdownMenu = $('[data-toggle="dropdown"]').siblings('.dropdown-menu'),

  caret = '&nbsp;<span class="icon-caret-down"></span>',

  setupSelect = function() {

    if ($(this).siblings('select')[0]) {

      return

    }

 

    var select = document.createElement('select'),

    defaultOption = document.createElement('option'),

    $initializer = $(this).siblings('[data-toggle="dropdown"]'),

    defaultValue = $initializer.data('default'),

    resetCollapse = function(evt) {

      var target, $target

      for (var i = 0; i < this.options.length; i++) {

        target = this.options[i].getAttribute('data-target')

        $target = $(target)

        if (this.options[i].value === this.value) {

          if ($target.hasClass('collapse')) {

            $($this.data('target')).collapse('show')

          } else if ($target.hasClass('collapse-alt')) {

            $($this.data('target')).show()

          } else if ($target.hasClass('tab-pane')) {

            $(this).attr('data-target', target).tab('show')

          }

        } else {

          $target.hasClass('collapse') && $target.hasClass('in') && $target.collapse('hide')

        }

      }

      $(this).trigger('updated.dropdown')

    }

 

    if (!$initializer.data('name')) {

      return

    }

 

    select.className = 'hidden-desktop'

    select.name = $initializer.data('name')

    select.id = select.name

    select.size = 1

 

    defaultOption.text = $initializer.text()

    defaultOption.value = defaultValue || ''

    select.appendChild(defaultOption)

    $(select).attr('data-rules', $initializer.data('rules'))

 

    $(this).find('li').each(function() {

      var option = document.createElement('option')

      option.text = $(this).find('a').text()

      option.value = $(this).find('a').data('value')

      $(option).attr('data-target', $(this).find('a').data('target'))

 

      select.appendChild(option)

    })

 

    $(select).on('change', resetCollapse)

 

    $(this).parents('.btn-group').append(select)

  }, updateDropDown = function(evt) {

    evt.preventDefault()

 

    var $this = $(this),

    target = $this.data('target'),

    resetCollapse = function() {

      $this

      .parents('.dropdown-menu')

      .find('a')

      .each(function() {

        if (!$(this).data('target')) {

          return

        }

 

        $($(this).data('target')).each(function() {

          toggle(this, 'hide')

        })

        $(this).parent().removeClass('active')

      })

    }, toggle = function(self, action) {

      if (typeof action !== 'string') {

        throw 'TypeError'
      }
      if ($(self).hasClass('collapse')) {
        $(self).collapse(action)
      } else if ($(self).hasClass('collapse-alt')) {
        $(self)[action]()
      } else if ($(self).hasClass('tab-pane')) {
        $this.tab('show')
      }
    }
    $this
    .parents('.dropdown-menu')
    .siblings('[data-toggle="dropdown"]')
    .text($this.text())
        .append(caret) //update dropdown
        $this
        .parents('.dropdown-menu')
        .siblings('select')
        .val($(evt.target).data('value')) //update hidden select box
        .trigger('updated.dropdown')

        if (target) {

          resetCollapse()

          $(target).each(function() {

            toggle(this, 'show')

          })

        } else {

          $($(this).parent().siblings().find('[data-target]').data('target')).each(function() {

          ($(this).hasClass('in') || $(this).is(':visible')) && toggle(this, 'hide')

        })

        }

      }
      $dropdownMenu.each(setupSelect)
      $('[data-toggle="dropdown"]').siblings('.dropdown-menu')
      $(document).on('touchend.dropdown click.dropdown', '[data-toggle="dropdown"] + .dropdown-menu a', updateDropDown)
      $('.modal').on('shown', function() {

        $(this).find($('[data-toggle="dropdown"]').siblings('.dropdown-menu')).each(setupSelect)

      })

  $(document).on('change.dropdown', '[data-toggle="dropdown"] ~ select', function(evt) {

    $(this).trigger('updated.dropdown')

    $(this).siblings('.dropdown-menu').find('[data-value=' + $(this).val() + ']').trigger('click.dropdown')

  })

}(window.jQuery)

jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker");
				var stripWidth = 1;
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar and Fabien Volpi
				});
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};

	jQuery.fn.showHide = function (options) {
 
    //default vars for the plugin
        var defaults = {
            speed: 1000,
            easing: '',
            changeText: 0,
            showText: 'Show',
            hideText: 'Hide'
 
        };
        var options = $.extend(defaults, options);
 
        $(this).hover(
        		function () {
             // this var stores which button you've clicked
             var toggleClick = $(this);
             // this reads the rel attribute of the button to determine which div id to toggle
             var toggleDiv = $(this).attr('title');
             // here we toggle show/hide the correct div at the right speed and using which easing effect
             $(toggleDiv).slideToggle(options.speed, options.easing, function() {
             // this only fires once the animation is completed
             if(options.changeText==1){
             $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
             }
              });
        },
        
        function () {
        	// optionally add the class .toggleDiv to each div you want to automatically close
        	 $('.toggleDiv').slideUp(options.speed, options.easing);
        	        });
 
    };
    
    (function($){
    	  $.extend({
    	    jGFeed : function(url, fnk, num, key){
    	      // Make sure url to get is defined
    	      if(url == null) return false;
    	      // Build Google Feed API URL
    	      var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;
    	      if(num != null) gurl += "&num="+num;
    	      if(key != null) gurl += "&key="+key;
    	      // AJAX request the API
    	      $.getJSON(gurl, function(data){
    	        if(typeof fnk == 'function')
    	                  fnk.call(this, data.responseData.feed);
    	                else
    	                  return false;
    	      });
    	    }
    	  });
    	})(jQuery);
